package com.ict.conf;


import com.ict.filter.ContentCachingFilter;
import com.ict.interceptor.LoggingInterceptor;
import com.ict.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

//    @Autowired
//    private PublicKey publicKey; // 注入公钥

    @Autowired
    private TokenInterceptor tokenInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoggingInterceptor())
                .addPathPatterns("/**");                 // 拦截所有路径
//                .excludePathPatterns("/swagger**");     // 排除Swagger

        registry.addInterceptor(tokenInterceptor)
                .excludePathPatterns("/ict-user/register", "/ict-user/loginWithCode", "/ict-user/loginVerification")  //配置不需要拦截的路径 登录路径
                .addPathPatterns("/**"); //配置拦击路径 非登录

//        registry.addInterceptor(new SignatureValidationInterceptor(publicKey))
//                .addPathPatterns("/**"); // 拦截所有请求
    }

    @Bean
    public FilterRegistrationBean<ContentCachingFilter> contentCachingFilter() {
        FilterRegistrationBean<ContentCachingFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new ContentCachingFilter());
        registration.addUrlPatterns("/*"); // 覆盖所有请求
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE); // 最高优先级
        return registration;
    }
}
